package main

import (
	"flag"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	"github.com/micro/go-plugins/wrapper/select/roundrobin/v2"
	"log"
	"rpc-service/user/config"
	"rpc-service/user/datasource"
	"rpc-service/user/handler"
)

var	configPath  = flag.String("config", "./config.ini", "-config file path")

func InitServer() {
	//解析命令运行参数
	flag.Parse()
	//从config.json加载配置信息
	log.Println("Config Path:", *configPath)
	config.InitConfig(*configPath)
	//连接数据库
	datasource.InitDatabase()
}

func main() {
	InitServer()
	conf := config.GetConfig()
	// 注册中心
	registry := etcd.NewRegistry(registry.Addrs(conf.RegistryAddr))
	// 负载均衡
	wrapper := roundrobin.NewClientWrapper()
	// 创建服务
	service := micro.NewService(
		micro.Address(":"+conf.Port),
		micro.Name(conf.ServiceName),
		micro.Version(conf.Version),
		micro.Registry(registry),
		micro.WrapClient(wrapper),
		)

	// 初始化服务
	service.Init()

	handler.RegisterServiceHandlers(service.Server())

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
